package com.zwface.demo;

import android.app.Activity;
import android.app.Application;
import android.app.ProgressDialog;
import android.os.Bundle;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.zwface.demo.comm.comm;
import com.zwface.demo.function.main.MainActivity;
import com.zwface.demo.greendao.DaoSession;
import com.zwface.demo.greendao.manager.DatabaseDaoManager;
import com.zwface.demo.greendao.manager.DatabasePath;
import com.zwface.demo.utils.SharedPreferenceUtils;

import org.greenrobot.greendao.query.QueryBuilder;


public class App extends Application
{
    private static App app = null;
    //加载框变量
    public ProgressDialog progressDialog;
    private MainActivity mainActivity;

    private int appCount = 0;
    private DaoSession mDaoSession;

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        initAppCount();
        initLogConfig();
        initGreenDao();
    }

    public static App getInstance()
    {
        return app;
    }

    public MainActivity getMainActivity() {
        return mainActivity;
    }

    public void setMainActivity(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
    }

    /**
     * log配置
     */
    private void initLogConfig()
    {
        String userId = SharedPreferenceUtils.getParam(this, SharedPreferenceUtils.USER_ID);
        if( !StringUtils.isEmpty(userId) ){
            String logDir = comm.BASE_DIR+userId+"/log/";
            FileUtils.createOrExistsDir(logDir);
            LogUtils.getConfig().setSaveDays(7);
            LogUtils.getConfig().setDir(logDir);
            LogUtils.getConfig().setLogSwitch(true);
        }
    }

    private void initAppCount()
    {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {
                appCount++;
            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {
                appCount--;
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }

    /**
     * app是否在前台
     * @return true前台，false后台
     */
    public boolean isForeground(){
        return appCount > 0;
    }


    public DaoSession getDaoSession() {
        return mDaoSession;
    }

    public void setDaoSession(DaoSession mDaoSession) {
        this.mDaoSession = mDaoSession;
    }

    private void initGreenDao(){
        DatabasePath.initDatabasePath(DatabasePath.DB_NAME);
        QueryBuilder.LOG_SQL = true;
        QueryBuilder.LOG_VALUES = true;
        DatabaseDaoManager.initSessions(this,DatabasePath.DB_NAME);
    }
}
